package customerEnter.Frame.spring;

import static com.google.common.collect.Maps.newHashMap;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import customerEnter.Frame.Utils.UrlParamEncryption;
import customerEnter.tool.WhetherToEmpty;

public class PropertyPlaceholderConfigurerAouto extends PropertyPlaceholderConfigurer {

	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		// 解密被加密過的參數
		decryption(props);
		// 加入到bean裡面
		super.processProperties(beanFactoryToProcess, props);
	}

	public void decryption(Properties props) {

		String publickey = props.getProperty("publickey");// 獲取密鑰
		Map<String, String> map = newHashMap();
		map.put("usernameTow", "usernameTow");
		map.put("passwordTow", "passwordTow");
		map.put("username", "username");
		map.put("password", "password");

		if (!WhetherToEmpty.StringWhetherToEmpty(publickey)) {
			for (Entry<String, String> entry : map.entrySet()) {
				String key = entry.getKey();
				String value = props.getProperty(key);
				String str = "";
				try {
					str = UrlParamEncryption.decryption(value, publickey);
				} catch (Exception e) {
					str = value;
				}
				props.put(key, str);
			}
		}

	}

}
